home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 2: Applications / Linux Cubed Series 2 - Applications.iso / math / gle-3.000 / gle-3 / gle / d_x.c < prev    next >
C/C++ Source or Header  |  1995-02-25  |  29KB  |  1,035 lines

  1. /*--------------------------------------------------------------*/
  2. /*    DecWindows Driver 1.0 , for GLE V3.0                 */
  3. /*--------------------------------------------------------------*/
  4. /*      NOTE: MUST be run from a DECwindow (vt100 window, or curses)     */
  5. /*---------------------------------------------------------------------------*/
  6. #include "all.h"
  7. #include <math.h>
  8. #include "core.h"
  9. #include "mygraph.h"
  10. #include "mydev.h"
  11. extern int gunit;
  12. extern struct gmodel g;
  13. /*---------------------------------------------------------------------------*/
  14.  
  15. void path_move(int x, int y);
  16. void path_line(int x, int y);
  17. void path_close(void);
  18. void path_fill(void);
  19. void path_newpath(void);
  20. void path_stroke(void);
  21.  
  22. /*---------------------------------------------------------------------------*/
  23. /*         XWindows stuff here         */
  24. /*-----------------------------------------------*/
  25.  
  26. /* Usage of backingstore should be *Always* safe now.... */
  27. #ifndef NOBACKINGSTORE
  28.    #define BACKINGSTORE
  29. #endif
  30.  
  31. #ifdef VMS
  32. #include <decw$include/Xlib.h>
  33. #include <decw$include/Xutil.h>
  34. #else
  35. #ifdef aix
  36. #define NeedFunctionPrototypes 0
  37. #endif
  38. #include <X11/Xlib.h>
  39. #include <X11/Xutil.h>
  40. #endif
  41.  
  42. #define FontName "-ADOBE-NEW CENTURY SCHOOLBOOK-MEDIUM-R-NORMAL--*-140-*-*-P-*"
  43. #define WindowName "CGLE Output"
  44. int maxxsize;
  45. int maxysize;
  46. int window1W;
  47. int window1H;
  48. Display *dpy;
  49. Window window1;
  50. GC gc,gcf;
  51. Screen *screen;
  52. int doesbackingstore;
  53. static void doInitialize();
  54. static int  doDefineColor(int n);
  55. static void doCreateWindows();
  56. static void doCreateGraphicsContext();
  57. static void doLoadFont();
  58. static void doExpose();
  59. static void doWMHints();
  60. static void doMapWindows();
  61. static void openDisplay();
  62.  
  63. /*
  64. static void doHandleEvents();
  65. static void doButtonPress();
  66. */
  67.  
  68. /******************** openDisplay ******************************************/
  69.  
  70. static void openDisplay()
  71. {
  72.     dpy = XOpenDisplay(0);
  73.     if (dpy == NULL){
  74.         gle_abort("Display not opened!\n");
  75.     scr_end();
  76.     }
  77.     screen = XDefaultScreenOfDisplay(dpy);
  78.     doesbackingstore = DoesBackingStore(screen);
  79. }
  80.  
  81. /***************** doInitialize **************************/
  82. int color_table[10];                /* a.r. 10 instead of 9 */
  83. static void doInitialize()
  84. {
  85.     int i;
  86.  
  87.     openDisplay();
  88.     doCreateWindows();
  89.  
  90.     for (i=0;i<=9; i++) {
  91.     color_table[i] = doDefineColor(i);
  92.     }
  93.  
  94.     doCreateGraphicsContext();
  95.  
  96.     XSync(dpy,False);
  97.  
  98.     doLoadFont();
  99.     doWMHints();
  100.     doMapWindows();
  101. }
  102.  
  103. /******* doCreateWindows *********/
  104. static void doCreateWindows()
  105. {
  106.     int window1X = (XWidthOfScreen(screen)-window1W);
  107.     int window1Y = 1;
  108.     XSetWindowAttributes xswa;
  109.     unsigned long CW;
  110.  
  111.     /* Create the window1 window   width and height are global variables*/
  112.  
  113.     xswa.event_mask = ExposureMask | ButtonPressMask | KeyPressMask
  114.             | VisibilityChangeMask;
  115.  
  116.     xswa.background_pixel = doDefineColor(0);
  117. #ifdef BACKINGSTORE
  118.     if (doesbackingstore){
  119.        xswa.backing_store = Always;
  120.        CW = CWEventMask | CWBackPixel  | CWBackingStore;
  121.     }
  122.     else
  123.        CW = CWEventMask | CWBackPixel;
  124. #else
  125.        CW = CWEventMask | CWBackPixel;
  126. #endif
  127.  
  128.     window1 = XCreateWindow(dpy,
  129.                 XRootWindowOfScreen(screen),
  130.                 window1X, window1Y, 
  131.                 window1W, window1H,
  132.                 0,
  133.                 XDefaultDepthOfScreen(screen), 
  134.                 InputOutput,
  135.                 XDefaultVisualOfScreen(screen),
  136.                 CW,
  137.                 &xswa);
  138. }
  139.  
  140.  
  141. /******** Create the graphics context *********/
  142. static void doCreateGraphicsContext()
  143. {
  144.     XGCValues xgcv;
  145.  
  146.     /* Create graphics context. */
  147.  
  148.     xgcv.background = doDefineColor(0);
  149.     xgcv.foreground = doDefineColor(1);
  150.  
  151.     gc  = XCreateGC(dpy, window1, GCForeground | GCBackground, &xgcv);
  152.     gcf = XCreateGC(dpy, window1, GCForeground | GCBackground, &xgcv);
  153. }
  154. setcolor(i)
  155. {
  156.     XGCValues xgcv;
  157.     xgcv.foreground = color_table[i];
  158.     XChangeGC(dpy, gc, GCForeground , &xgcv);
  159. }
  160. setfillcolor(i)
  161. {
  162.     XGCValues xgcv;
  163.     xgcv.foreground = color_table[i];
  164.     XChangeGC(dpy, gcf, GCForeground , &xgcv);
  165. }
  166.  
  167. setfillstyle(i)
  168. {
  169. #define BM_WIDTH    16
  170. #define BM_HEIGHT    16
  171. Pixmap bm;
  172. unsigned char bm_bits[][32] = {
  173. {  0x00, 0x80, 0x00, 0x40, 0x00, 0x20, 0x00, 0x10, 0x00, 0x08, 0x00, 0x04,
  174.    0x00, 0x02, 0x00, 0x01, 0x80, 0x00, 0x40, 0x00, 0x20, 0x00, 0x10, 0x00,
  175.    0x08, 0x00, 0x04, 0x00, 0x02, 0x00, 0x01, 0x00},  /* SHADE    */ 
  176. {  0x11, 0x11, 0x88, 0x88, 0x44, 0x44, 0x22, 0x22, 0x11, 0x11, 0x88, 0x88,
  177.    0x44, 0x44, 0x22, 0x22, 0x11, 0x11, 0x88, 0x88, 0x44, 0x44, 0x22, 0x22,
  178.    0x11, 0x11, 0x88, 0x88, 0x44, 0x44, 0x22, 0x22},  /* SHADE  1 */
  179. {  0x01, 0x01, 0x80, 0x80, 0x40, 0x40, 0x20, 0x20, 0x10, 0x10, 0x08, 0x08,
  180.    0x04, 0x04, 0x02, 0x02, 0x01, 0x01, 0x80, 0x80, 0x40, 0x40, 0x20, 0x20,
  181.    0x10, 0x10, 0x08, 0x08, 0x04, 0x04, 0x02, 0x02},  /* SHADE  2 */
  182. {  0x33, 0x33, 0x99, 0x99, 0xcc, 0xcc, 0x66, 0x66, 0x33, 0x33, 0x99, 0x99,
  183.    0xcc, 0xcc, 0x66, 0x66, 0x33, 0x33, 0x99, 0x99, 0xcc, 0xcc, 0x66, 0x66,
  184.    0x33, 0x33, 0x99, 0x99, 0xcc, 0xcc, 0x66, 0x66},  /* SHADE  3 */
  185. {  0x07, 0x07, 0x83, 0x83, 0xc1, 0xc1, 0xe0, 0xe0, 0x70, 0x70, 0x38, 0x38,
  186.    0x1c, 0x1c, 0x0e, 0x0e, 0x07, 0x07, 0x83, 0x83, 0xc1, 0xc1, 0xe0, 0xe0,
  187.    0x70, 0x70, 0x38, 0x38, 0x1c, 0x1c, 0x0e, 0x0e},  /* SHADE  4 */
  188. {  0x0f, 0x0f, 0x87, 0x87, 0xc3, 0xc3, 0xe1, 0xe1, 0xf0, 0xf0, 0x78, 0x78,
  189.    0x3c, 0x3c, 0x1e, 0x1e, 0x0f, 0x0f, 0x87, 0x87, 0xc3, 0xc3, 0xe1, 0xe1,
  190.    0xf0, 0xf0, 0x78, 0x78, 0x3c, 0x3c, 0x1e, 0x1e},  /* SHADE  5 */
  191. {  0x01, 0x80, 0x02, 0x40, 0x04, 0x20, 0x08, 0x10, 0x10, 0x08, 0x20, 0x04,
  192.    0x40, 0x02, 0x80, 0x01, 0x80, 0x01, 0x40, 0x02, 0x20, 0x04, 0x10, 0x08,
  193.    0x08, 0x10, 0x04, 0x20, 0x02, 0x40, 0x01, 0x80},  /* GRID     */
  194. {  0x01, 0x01, 0x82, 0x82, 0x44, 0x44, 0x28, 0x28, 0x10, 0x10, 0x28, 0x28,
  195.    0x44, 0x44, 0x82, 0x82, 0x01, 0x01, 0x82, 0x82, 0x44, 0x44, 0x28, 0x28,
  196.    0x10, 0x10, 0x28, 0x28, 0x44, 0x44, 0x82, 0x82},  /* GRID   1 */
  197. {  0x01, 0x01, 0x82, 0x82, 0x44, 0x44, 0x28, 0x28, 0x10, 0x10, 0x28, 0x28,
  198.    0x44, 0x44, 0x82, 0x82, 0x01, 0x01, 0x82, 0x82, 0x44, 0x44, 0x28, 0x28,
  199.    0x10, 0x10, 0x28, 0x28, 0x44, 0x44, 0x82, 0x82},  /* GRID   2 */
  200. {  0x83, 0x83, 0xc6, 0xc6, 0x6c, 0x6c, 0x38, 0x38, 0x38, 0x38, 0x6c, 0x6c,
  201.    0xc6, 0xc6, 0x83, 0x83, 0x83, 0x83, 0xc6, 0xc6, 0x6c, 0x6c, 0x38, 0x38,
  202.    0x38, 0x38, 0x6c, 0x6c, 0xc6, 0xc6, 0x83, 0x83},  /* GRID   3 */
  203. {  0x03, 0xc0, 0x06, 0x60, 0x0c, 0x30, 0x18, 0x18, 0x30, 0x0c, 0x60, 0x06,
  204.    0xc0, 0x03, 0x80, 0x01, 0xc0, 0x03, 0x60, 0x06, 0x30, 0x0c, 0x18, 0x18,
  205.    0x0c, 0x30, 0x06, 0x60, 0x03, 0xc0, 0x01, 0x80},  /* GRID   4 */
  206. {  0x03, 0xc0, 0x07, 0xe0, 0x0e, 0x70, 0x1c, 0x38, 0x38, 0x1c, 0x70, 0x0e,
  207.    0xe0, 0x07, 0xc0, 0x03, 0xc0, 0x03, 0xe0, 0x07, 0x70, 0x0e, 0x38, 0x1c,
  208.    0x1c, 0x38, 0x0e, 0x70, 0x07, 0xe0, 0x03, 0xc0}   /* GRID   5 */
  209. };
  210.  
  211.     bm = XCreateBitmapFromData(dpy,window1,bm_bits[i],BM_WIDTH,BM_HEIGHT);
  212.     XSetStipple(dpy, gcf, bm);
  213.     XSetFillStyle(dpy, gcf, FillStippled);
  214.     XFreePixmap(dpy, bm);
  215. }
  216.  
  217.  
  218. /******* Load the font for text writing ******/
  219. static void doLoadFont()
  220. {
  221.     Font font;
  222.  
  223.     font = XLoadFont(dpy, FontName);
  224.     XSetFont(dpy, gc, font);
  225. }
  226.  
  227. /******* Create color ************************/
  228. static int doDefineColor(int i)
  229. {
  230.     int pixel;
  231.     char *colors[] = {
  232.     "white",
  233.     "black",
  234.     "red",
  235.     "green",
  236.     "blue",
  237.     "yellow",
  238.     "magenta",
  239.     "cyan",
  240.     "light grey",
  241.     "dim grey"                /* a.r. instead of black */
  242.     };
  243.     XColor exact_color,screen_color;
  244.     if ((XDefaultVisualOfScreen(screen))->class == PseudoColor
  245.         ||  (XDefaultVisualOfScreen(screen))->class == DirectColor)
  246.         if (XAllocNamedColor(dpy, XDefaultColormapOfScreen(screen),
  247.             colors[i], &screen_color, &exact_color))
  248.                 return screen_color.pixel;
  249.             else
  250.             printf("Color not allocated! {%s}\n",colors[i]);
  251.      else
  252.         switch (i) {
  253.             case 0:        return XWhitePixelOfScreen(screen); break;
  254.         case 1:        return XBlackPixelOfScreen(screen); break;
  255.             default:        return XBlackPixelOfScreen(screen); break;
  256.         }
  257. }
  258.  
  259. /******** do WMHints *************/
  260. static void doWMHints()
  261. {
  262.     XSizeHints xsh;
  263.  
  264.     /* Define the size and name of the window1 window */
  265.  
  266.     xsh.x = XWidthOfScreen(screen)-window1W;
  267.     xsh.y = 1;
  268.     xsh.width  = xsh.min_width  = xsh.max_width  = window1W;
  269.     xsh.height = xsh.min_height = xsh.max_height = window1H;
  270.     xsh.flags = PSize | PMinSize | PMaxSize | PPosition | USPosition;
  271.  
  272.     XSetNormalHints(dpy, window1, &xsh);
  273.  
  274.     XStoreName(dpy, window1, WindowName);
  275. }
  276. /******** doMapWindows ***********/
  277. static void doMapWindows()
  278. {
  279.     set_expose();
  280.     XMapWindow(dpy, window1);
  281. }
  282.  
  283. getmaxx()
  284. {
  285.     return XHeightOfScreen( screen );            /* a.r. */
  286. }
  287. getmaxy()
  288. {
  289.     return (XHeightOfScreen( screen ) * 0.95 );        /* a.r. */
  290.     /* how do I query the height of a titlebar from the Window Manager ? */
  291. }
  292. getmaxcolor()
  293. {
  294.     return 10;                /* a.r. 10 instead of 9 */
  295. }
  296. /*---------------------------------------------------------------------------*/
  297.  
  298. #define DASHED_LINE 2
  299. #define SOLID_LINE 1
  300. #define pi 3.141592653
  301. #define BLACKANDWHITE 1
  302. #define false 0
  303. #define true (!false)
  304. #define dbg if ((gle_debug & 64)>0)
  305.  
  306. double savexsize,saveysize;
  307. extern int gle_debug;
  308. int gle_nspeed = 2; /* text mode = slow and fast */
  309. int incap=true;
  310. /*---------------------------------------------------------------------------*/
  311. /* The global variables for the PC screen driver */
  312. /*-----------------------------------------------*/
  313. int i,l,j,ix,iy;
  314. double f;
  315. double xsizecm,ysizecm;
  316. #define sx(v) ( (int) ((v) * d_xscale))
  317. #define sy(v) ( d_maxy - ((int) ((v) * d_yscale)))
  318. #define rx(v) ( (int) ((v) * d_xscale))
  319. #define ry(v) ( d_maxy - ((int) ((v) * d_yscale)))
  320.  
  321. double d_scale, d_xscale, d_yscale;
  322. int d_graphmode;
  323. int d_fillstyle=1,d_fillcolor;
  324. int d_lstyle,d_lwidth;
  325. int d_maxy;
  326.  
  327. d_devcmd(char *s)
  328. {}
  329. dxy(double x, double y, int *dx, int *dy)
  330. {
  331.     static double fx,fy;
  332.     g_dev(x,y,&fx,&fy);
  333.     *dx = sx(fx);
  334.     *dy = sy(fy);
  335. }
  336.  
  337. rxy(double x, double y, int *dx, int *dy)
  338. {
  339.     static double fx,fy,zx,zy;
  340.     g_dev(x,y,&fx,&fy);
  341.     g_dev(0.0,0.0,&zx,&zy);
  342.     *dx = (int) ( (fx-zx) * d_xscale);
  343.     *dy = (int) ( (fy-zy) * d_yscale);
  344. }
  345.  
  346. /* short for XFillPolygon() */
  347. dxy_short(double x, double y, short *dx, short *dy)
  348. {
  349.     static double fx,fy;
  350.     g_dev(x,y,&fx,&fy);
  351.     *dx = sx(fx);
  352.     *dy = sy(fy);
  353. }
  354. rxy_short(double x, double y, short *dx, short *dy)
  355. {
  356.     static double fx,fy,zx,zy;
  357.     g_dev(x,y,&fx,&fy);
  358.     g_dev(0.0,0.0,&zx,&zy);
  359.     *dx = (int) ( (fx-zx) * d_xscale);
  360.     *dy = (int) ( (fy-zy) * d_yscale);
  361. }
  362.  
  363. /*---------------------------------------------------------------------------*/
  364. d_dfont(char *c)
  365. {
  366.     /* only used for the DFONT driver which builds fonts */
  367. }
  368. /*---------------------------------------------------------------------------*/
  369. static char lastline[80];
  370. d_message(char *s)
  371. {
  372.     w_message(s);
  373. }
  374. /*---------------------------------------------------------------------------*/
  375. d_source(char *s)
  376. {
  377.     s=s;
  378. }
  379. /*---------------------------------------------------------------------------*/
  380. d_get_type(char *t)
  381. {
  382.     strcpy(t,"INTERACTIVE, X, DECWINDOWS, XWINDOWS");
  383. }
  384. /*---------------------------------------------------------------------------*/
  385. d_set_path(int onoff)
  386. {}
  387. /*---------------------------------------------------------------------------*/
  388. d_newpath()
  389. {
  390.     path_newpath();
  391. }
  392. /*---------------------------------------------------------------------------*/
  393. d_open(double width, double height)
  394. {
  395.     gle_nspeed = 2; /* text mode = slow and fast */
  396.     xsizecm = 16;
  397.     ysizecm = 16*0.95;
  398.     /* Get largest rectangle we can fit on the screen */
  399.     d_scale = xsizecm / width;
  400.     f = ysizecm / height;
  401.     if (f<d_scale) d_scale = f;
  402.     if (dpy==NULL) 
  403.         openDisplay();
  404.     d_xscale = d_scale * (getmaxx()) / xsizecm; /* Device Scale X, Device Scale y */
  405.     d_yscale = d_scale * (getmaxy()) / ysizecm;
  406.         window1W = width*d_xscale;
  407.         window1H = height*d_yscale;
  408.     d_maxy = window1H;
  409.     if (dpy==NULL || savexsize != width || saveysize != height) {
  410.         if (dpy != NULL) 
  411.                    {
  412.                      if (window1 != 0)
  413.                         {
  414.              XUnmapWindow(dpy, window1);
  415.              XDestroyWindow(dpy, window1);
  416.                         }
  417.             XCloseDisplay(dpy);
  418.                    }
  419.         openDisplay();
  420.         doInitialize();
  421.         set_expose();
  422.         XRaiseWindow(dpy, window1);
  423.         wait_expose();
  424.     } else {
  425.         set_expose();
  426.         XRaiseWindow(dpy, window1);
  427. #ifndef BACKINGSTORE                        /* a.r. */
  428.         wait_expose();
  429. #else
  430.                 if (!doesbackingstore)
  431.                    wait_expose();
  432. #endif
  433.     }
  434.         XClearWindow(dpy, window1);
  435.     XSync(dpy,False);
  436.     savexsize = width;
  437.     saveysize = height;
  438. }
  439. /*---------------------------------------------------------------------------*/
  440. d_tidyup()
  441. {
  442. }
  443. d_close()
  444. {
  445.     int32 emask;
  446.     XEvent ereturn;
  447.         Window rr, cr;
  448.         int rx, ry, cx, cy;
  449.         unsigned int retmask;
  450.         
  451.     g_flush();
  452.     lastline[0] = 0;
  453.     XSync(dpy,False);
  454.     
  455.        XQueryPointer(dpy, window1, &rr, &cr, &rx, &ry, &cx, &cy, &retmask);
  456.        if ( cx >= 0 &&  cx <= window1W && cy >= 0 &&  cy <= window1H )
  457.            {
  458.             fner("Picture completed, press any key or click mouse in the graphics window        ");
  459.             scr_refresh();
  460.  
  461.             emask = ButtonPressMask | KeyPressMask;
  462.             XWindowEvent(dpy, window1, emask, &ereturn);
  463.            }
  464.         else
  465.            {
  466.             fner("Picture completed, press RETURN to continue                    (Press any key)");
  467.             scr_refresh();
  468.  
  469.             text_inkey(); 
  470.            }
  471.     XLowerWindow(dpy, window1);
  472.         XSync(dpy,True);
  473. }
  474. /*---------------------------------------------------------------------------*/
  475. d_set_line_cap(int i)
  476. {
  477.    /*  lcap, 0= butt, 1=round, 2=projecting square */
  478. /* if X11 would do the same as Postscript......
  479.    XGCValues xgcv;
  480.    
  481.    xgcv.cap_style = i+1;
  482.    XChangeGC(dpy, gc,  GCCapStyle , &xgcv);
  483.    XChangeGC(dpy, gcf, GCCapStyle , &xgcv);
  484. */
  485. }
  486. /*---------------------------------------------------------------------------*/
  487. d_set_line_join(int i)
  488. {
  489.    /* 0= mitre, 1=round, 2=bevel */
  490. /* if X11 would do the same as Postscript......
  491.    XGCValues xgcv;
  492.    
  493.    xgcv.join_style = i;
  494.    XChangeGC(dpy, gc,  GCJoinStyle, &xgcv);
  495.    XChangeGC(dpy, gcf, GCJoinStyle, &xgcv);
  496. */
  497. }
  498. /*---------------------------------------------------------------------------*/
  499. d_set_line_miterlimit(double d)
  500. {
  501.     i++;
  502. }
  503. /*---------------------------------------------------------------------------*/
  504. d_set_line_width(double w)
  505. {
  506.     int xa,xb;
  507.     XGCValues xgcv;
  508.     rxy(w,w,&xa,&xb);
  509.     rxy(w,w,&xa,&xb);
  510.     xa = abs(xa);
  511.         xgcv.line_width = xa;
  512.         XChangeGC(dpy, gc, GCLineWidth , &xgcv);
  513. }
  514. /*---------------------------------------------------------------------------*/
  515. d_set_line_styled(double dd)
  516. {}
  517. d_set_line_style(char *s)
  518. {
  519.     XGCValues xgcv;
  520.         char *defline[] = {"", "", "12", "41", "14", "92", "1282", "9229",
  521.                        "4114", "54"};
  522.     int i, dashoff=0;
  523.     char dashlist[64];
  524.  
  525.     if (strlen(s)==1)  s = defline[*s-'0'];
  526.     if (strcmp(s,"")==0) {
  527.         xgcv.line_style  = LineSolid;
  528.         XChangeGC(dpy, gc, GCLineStyle , &xgcv);
  529.     } else {
  530.         xgcv.line_style  = LineDoubleDash;
  531.         XChangeGC(dpy, gc, GCLineStyle , &xgcv);
  532.         for (i=0; *s!=0; s++,i++)
  533.         dashlist[i] = *s-'0' ? *s-'0' : 1;
  534.         XSetDashes(dpy, gc, dashoff, dashlist, i);
  535.     }
  536. }
  537. /*---------------------------------------------------------------------------*/
  538. int in_font;
  539. d_fill()
  540. {
  541.     if (in_font)
  542.         path_stroke();
  543.     else
  544.         path_fill();
  545. }
  546. /*---------------------------------------------------------------------------*/
  547. d_fill_ary(int nwk,double (*wkx)[],double (*wky)[])
  548. {
  549.     int i;
  550. /*    fprintf(psfile,"%g %g moveto \n",(*wkx)[0],(*wky)[0]);
  551.     for (i=1;i<nwk;i++)
  552.         fprintf(psfile,"%g %g l \n",(*wkx)[i],(*wky)[i]);
  553. */
  554. }
  555. d_line_ary(int nwk,double (*wkx)[],double (*wky)[])
  556. {
  557.     int i;
  558.     dxy( (*wkx)[0], (*wky)[0], &ix, &iy);
  559. /*    moveto(ix,iy); */
  560.     for (i=1;i<nwk;i++) {
  561.         dxy( (*wkx)[i], (*wky)[i], &ix, &iy);
  562.     }
  563. }
  564. /*---------------------------------------------------------------------------*/
  565. d_stroke()
  566. {
  567.     path_stroke();
  568. }
  569. /*---------------------------------------------------------------------------*/
  570. d_clip()
  571. {
  572. }
  573. /*---------------------------------------------------------------------------*/
  574. d_set_matrix(double newmat[3][3])
  575. {
  576. }
  577. /*---------------------------------------------------------------------------*/
  578. d_move(double zx,double zy)
  579. {
  580. }
  581. /*---------------------------------------------------------------------------*/
  582. d_reverse()     /* reverse the order of stuff in the current path */
  583. {
  584. }
  585. /*---------------------------------------------------------------------------*/
  586. d_closepath()
  587. {
  588.     if (g.inpath==true) {
  589.         path_close();
  590.         return;
  591.     }
  592.     g_line(g.closex,g.closey);
  593. }
  594. /*---------------------------------------------------------------------------*/
  595. d_line(double zx,double zy)
  596. {
  597.     static int ux,uy;
  598.     dxy(g.curx,g.cury,&ux,&uy);
  599.     dxy(zx,zy,&ix,&iy);
  600.     if (g.inpath==true) {
  601.         if (!g.xinline) path_move(ux,uy);
  602.         path_line(ix,iy);
  603.         return;
  604.     }
  605.         XDrawLine(dpy,window1,gc,ux,uy,ix,iy);
  606. }
  607. /*---------------------------------------------------------------------------*/
  608. d_clear()
  609. {
  610. }
  611. /*---------------------------------------------------------------------------*/
  612. d_flush()
  613. {
  614. }
  615. /*---------------------------------------------------------------------------*/
  616. int polar_xy(double r, double angle, double *dx, double *dy);
  617. int xy_polar(double dx,double dy,double *radius,double *angle);
  618.  
  619. #ifdef OLDARC /* draws weird things */
  620. d_arcto(dbl x1,dbl y1,dbl x2,dbl y2,dbl rrr)
  621. {
  622.     double x0,y0,r1,a1,r2,a2,r3,a3,a4,r5,ssx,ssy,ex,ey;
  623.     double bx1,by1,bx2,by2,dist,neg;
  624.     g_get_xy(&x0,&y0);
  625.     xy_polar(x1-x0,y1-y0,&r1,&a1);
  626.     xy_polar(x2-x1,y2-y1,&r2,&a2);
  627.     neg = 1;
  628.     a4 = (180-a2+a1);
  629.     a3 = a2 + (a4/2);
  630.     if ((a4/2)>90 && (a4/2)<180 ) neg = -1;
  631.     if ((a4/2)<0 && (a4/2)>-90 ) neg = -1;
  632.     r3 = neg*rrr/(tan((pi/180)*a4/2));
  633.     dbg gprint("rrr %g a4/2 %g t=%g a2=%g a1=%g r1=%g r2=%g r3=%g \n",rrr,a4/2,tan(a4/2),a2,a1,r1,r2,r3);
  634.     polar_xy(-r3,a1,&ssx,&ssy); ssx += x1; ssy += y1;
  635.     polar_xy(r3,a2,&ex,&ey); ex += x1; ey += y1;
  636.     g_line(ssx,ssy);
  637.     dist = sqrt((ex-ssx) * (ex-ssx) + (ey-ssy)*(ey-ssy));
  638.     polar_xy(r1+ dist/2.5-r3,a1,&bx1,&by1); bx1 += x0; by1 += y0;
  639.     polar_xy(-r2+ -dist/2.5+r3,a2,&bx2,&by2); bx2 += x2; by2 += y2;
  640.     g_bezier(bx1,by1,bx2,by2,ex,ey);
  641.     g_line(x2,y2);
  642. }
  643. /*---------------------------------------------------------------------------*/
  644. d_arc(dbl r,dbl t1,dbl t2,dbl cx,dbl cy)
  645. {
  646.     static int ixr,iyr,icx,icy,a1,a2;
  647.     double ux,uy,z;
  648.     dxy(cx,cy,&icx,&icy);
  649.     rxy(r,r,&ixr,&iyr);
  650.     if (t1>t2) {z = t1; t1 = t2; t2 = z;}
  651.     a1 = t1*64; a2 = t2*64 - a1;
  652.         XDrawArc(dpy,window1,gc,icx-ixr,icy-iyr,ixr*2,iyr*2,a1,a2);
  653. }
  654. /*---------------------------------------------------------------------------*/
  655. d_narc(dbl r,dbl t1,dbl t2,dbl cx,dbl cy)
  656. {
  657.     static int ixr,iyr,icx,icy,a1,a2;
  658.     double ux,uy,z;
  659.     dxy(cx,cy,&icx,&icy);
  660.     rxy(r,r,&ixr,&iyr);
  661.     if (t1>t2) {z = t1; t1 = t2; t2 = z;}
  662.     a1 = t1*64; a2 = t2*64 - a1;
  663.         XDrawArc(dpy,window1,gc,icx-ixr,icy-iyr,ixr*2,iyr*2,a1,a2);
  664. }
  665. /*---------------------------------------------------------------------------*/
  666. #else /* arc code from easydev.c */
  667. #define CSTEP (360/6)
  668. df_arcto(dbl x1,dbl y1,dbl x2,dbl y2,dbl rrr)
  669. {
  670.     double x0,y0,r1,a1,r2,a2,r3,a3,a4,r5,sx,sy,ex,ey;
  671.     double bx1,by1,bx2,by2,dist,neg;
  672.     g_get_xy(&x0,&y0);
  673.     xy_polar(x1-x0,y1-y0,&r1,&a1);
  674.     xy_polar(x2-x1,y2-y1,&r2,&a2);
  675.     neg = 1;
  676.     a4 = (180-a2+a1);
  677.     a3 = a2 + (a4/2);
  678.     if ((a4/2)>90 && (a4/2)<180 ) neg = -1;
  679.     if ((a4/2)<0 && (a4/2)>-90 ) neg = -1;
  680.     r3 = neg*rrr/(tan((pi/180)*a4/2));
  681.     polar_xy(-r3,a1,&sx,&sy); sx += x1; sy += y1;
  682.     polar_xy(r3,a2,&ex,&ey); ex += x1; ey += y1;
  683.     g_line(sx,sy);
  684.     dist = sqrt((ex-sx)*(ex-sx) + (ey-sy)*(ey-sy));
  685.     polar_xy(r1+ dist/2.5-r3,a1,&bx1,&by1); bx1 += x0; by1 += y0;
  686.     polar_xy(-r2+ -dist/2.5+r3,a2,&bx2,&by2); bx2 += x2; by2 += y2;
  687.     g_bezier(bx1,by1,bx2,by2,ex,ey);
  688.     g_line(x2,y2);
  689. }
  690. df_arc(dbl r,dbl t1,dbl t2,dbl cx,dbl cy)
  691. {
  692.     /* circle from t1 to t2, lets use 6 bezier's for a circle */
  693.     double stz;
  694.     int nst,i;
  695.  
  696.     for (;t2<t1;)
  697.           t2 = t2 + 360;
  698.  
  699.     nst = floor((t2-t1)/CSTEP)+1;
  700.     stz = (t2-t1) / nst;
  701.     for (i=1;i<=nst;i++)
  702.         xdf_barc(r,t1+stz*(i-1),t1+stz*i,cx,cy);
  703. }
  704. xdf_barc(double r,dbl t1,dbl t2,dbl cx,dbl cy)
  705. {
  706.     double rx1,ry1,rx2,ry2,d,dx1,dy1,dx2,dy2;
  707.  
  708.     polar_xy(r,t1,&rx1,&ry1);
  709.     polar_xy(r,t2,&rx2,&ry2);
  710.     d = sqrt( (rx2-rx1)*(rx2-rx1) + (ry2-ry1)*(ry2-ry1));
  711.     polar_xy(d/3,t1+90,&dx1,&dy1);
  712.     polar_xy(d/3,t2-90,&dx2,&dy2);
  713.     if (g.inpath) {
  714.         g_line(rx1+cx,ry1+cy);
  715.         g_bezier(rx1+cx+dx1,ry1+cy+dy1
  716.             ,rx2+cx+dx2,ry2+cy+dy2,rx2+cx,ry2+cy);
  717.     } else {
  718.         g_move(rx1+cx,ry1+cy);
  719.         g_bezier(rx1+cx+dx1,ry1+cy+dy1
  720.             ,rx2+cx+dx2,ry2+cy+dy2,rx2+cx,ry2+cy);
  721.         g_move(cx,cy);
  722.     }
  723. }
  724.  
  725. d_arcto(dbl x1,dbl y1,dbl x2,dbl y2,dbl rrr)
  726. {
  727.     df_arcto(x1,y1,x2,y2,rrr);
  728. }
  729. /*---------------------------------------------------------------------------*/
  730. d_arc(dbl r,dbl t1,dbl t2,dbl cx,dbl cy)
  731. {        
  732.     df_arc(r,t1,t2,cx,cy);
  733. }
  734. /*---------------------------------------------------------------------------*/
  735. d_narc(dbl r,dbl t1,dbl t2,dbl cx,dbl cy)
  736. {
  737.     /* swap t1 and t2 */
  738.     df_arc(r,t2,t1,cx,cy);
  739. }
  740. /*---------------------------------------------------------------------------*/
  741. #endif /* OLDARC */
  742. /*---------------------------------------------------------------------------*/
  743.  
  744. d_box_fill(dbl x1, dbl y1, dbl x2, dbl y2)
  745. {
  746. #ifdef OLDARC
  747.     static int ix1,iy1,ix2,iy2;
  748.     static int ii;
  749.     dxy(x1,y1,&ix1,&iy1);
  750.     dxy(x2,y2,&ix2,&iy2);
  751.     if (ix1>ix2) { ii = ix1; ix1 = ix2; ix2 = ii; }
  752.     if (iy1>iy2) { ii = iy1; iy1 = iy2; iy2 = ii; }
  753.         XFillRectangle(dpy,window1,gcf,ix1,iy1,ix2-ix1,iy2-iy1);
  754. #else
  755.     XPoint point[4];
  756.     int n = 0;
  757.  
  758.         dxy_short(x1,y1,&point[n].x,&point[n].y); n++;
  759.         dxy_short(x2,y1,&point[n].x,&point[n].y); n++;
  760.         dxy_short(x2,y2,&point[n].x,&point[n].y); n++;
  761.         dxy_short(x1,y2,&point[n].x,&point[n].y);
  762.  
  763.        XFillPolygon(dpy,window1,gcf,point,4,Convex,CoordModeOrigin);
  764. #endif
  765. }
  766. d_box_stroke(dbl x1, dbl y1, dbl x2, dbl y2)
  767. {
  768.        g_move(x1,y1);
  769.         g_line(x2,y1);
  770.         g_line(x2,y2);
  771.         g_line(x1,y2);
  772.         g_line(x1,y1);
  773. }
  774. /*---------------------------------------------------------------------------*/
  775. d_circle_stroke(double zr)
  776. {
  777. #ifdef OLDARC
  778.     static int ixr,iyr;
  779.     int ux,uy;
  780.     rxy(zr,zr,&ixr,&iyr);
  781.     ixr =(fabs(ixr)+fabs(iyr))/2;
  782.     dxy(g.curx,g.cury,&ux,&uy);
  783.         XDrawArc(dpy,window1,gc,ux-ixr,uy-ixr,ixr*2,ixr*2,0,64*360);
  784. #else
  785.     d_arc(zr,0,360,g.curx,g.cury);
  786. #endif
  787. }
  788. d_circle_fill(double zr)
  789. {
  790. #ifdef OLDARC
  791.     static int ixr,iyr;
  792.     int rr;
  793.     int ux,uy;
  794.     rxy(zr,zr,&ixr,&iyr);
  795.     rr = (fabs(ixr)+fabs(iyr))/2;
  796.     dxy(g.curx,g.cury,&ux,&uy);
  797.         XFillArc(dpy,window1,gcf,ux-rr,uy-rr,rr*2,rr*2,0,64*360);
  798. #else
  799.     if (!g.inpath){
  800.         g_set_path(true);
  801.         g_newpath();
  802.         g_arc(zr,0,360,g.curx,g.cury);
  803.         g_closepath();
  804.         g_fill();
  805.         g_set_path(false);
  806.     }
  807.     else
  808.         g_arc(zr,0,360,g.curx,g.cury);
  809. #endif
  810. }
  811. /*---------------------------------------------------------------------------*/
  812. d_bezier(dbl x1,dbl y1,dbl x2,dbl y2,dbl x3,dbl y3)
  813. {
  814.     double ax,bx,cx,ay,by,cy,dist;
  815.     double xxx,yyy,i,t,nstep,x0,y0;
  816.     g_get_xy(&x0,&y0);
  817.     dist = fabs(x3-x0) + fabs(y3-y0);
  818.     nstep = 12;
  819.     if (dist<1) nstep = 7;
  820.     if (dist<.5) nstep = 3;
  821.     if (dist<.1) {
  822.         g_line(x3,y3);
  823.         return;
  824.     }
  825.     cx = (x1-x0)*3;
  826.     bx = (x2-x1)*3-cx;
  827.     ax = x3-x0-cx-bx;
  828.     cy = (y1-y0)*3;
  829.     by = (y2-y1)*3-cy;
  830.     ay = y3-y0-cy-by;
  831.     for (i=0;i<=nstep;i++) {
  832.         t = i/nstep;
  833.         xxx = ax*pow(t,3.0) + bx*t*t + cx*t + x0;
  834.         yyy = ay*pow(t,3.0) + by*t*t + cy*t + y0;
  835.         g_line(xxx,yyy);
  836.     }
  837. }
  838. /*---------------------------------------------------------------------------*/
  839. d_set_color(int32 f)            /* colors slightly changed a.r. */
  840. {
  841.     int i;
  842.     colortyp  cc;
  843.     cc.l = f;
  844.     i = 1;
  845.     if (cc.b[B_R]>=10 && cc.b[B_G]>=10 && cc.b[B_B]>=10)     i = 9;
  846.     if (cc.b[B_R]>60  && cc.b[B_G]>60  && cc.b[B_B]>60)      i = 8;
  847.     if (cc.b[B_R]>100)                     i = 2;
  848.     if (cc.b[B_B]>100)                     i = 4;
  849.     if (cc.b[B_G]>100)                     i = 3;
  850.     if (cc.b[B_R]>100 && cc.b[B_G]>100)             i = 5;
  851.     if (cc.b[B_G]>100 && cc.b[B_B]>100)             i = 7;
  852.     if (cc.b[B_R]>30  && cc.b[B_B]>100)             i = 6;
  853.     if (cc.b[B_R]>100 && cc.b[B_G]>100 && cc.b[B_B]>100)     i = 8;
  854.     if (cc.b[B_R]<10  && cc.b[B_G]<10  && cc.b[B_B]<10)     i = 1; 
  855.     if (cc.b[B_R]>250 && cc.b[B_G]>250 && cc.b[B_B]>250)     i = 0;
  856.         
  857.     setcolor(i);
  858. }
  859. d_set_fill(int32 f)            /* colors slightly changed a.r. */
  860. {
  861.     int i, j;
  862.     colortyp  cc;
  863.     cc.l = f;
  864.     i = 1;
  865.        j = 0;
  866.         if (cc.b[B_F] == 1)     /* colours */
  867.            {
  868.                if (cc.b[B_R]>=10 && cc.b[B_G]>=10 && cc.b[B_B]>=10)     i = 9;
  869.                 if (cc.b[B_R]>60  && cc.b[B_G]>60  && cc.b[B_B]> 60)      i = 8;
  870.                 if (cc.b[B_R]>100)                     i = 2;
  871.                 if (cc.b[B_B]>100)                     i = 4;
  872.                 if (cc.b[B_G]>100)                     i = 3;
  873.                 if (cc.b[B_R]>100 && cc.b[B_G]>100)             i = 5;
  874.                 if (cc.b[B_G]>100 && cc.b[B_B]>100)             i = 7;
  875.                 if (cc.b[B_R]>30  && cc.b[B_B]>100)             i = 6;
  876.                 if (cc.b[B_R]>100 && cc.b[B_G]>100 && cc.b[B_B]>100)     i = 8;
  877.                 if (cc.b[B_R]<10  && cc.b[B_G]<10  && cc.b[B_B]< 10)     i = 1; 
  878.                 if (cc.b[B_R]>250 && cc.b[B_G]>250 && cc.b[B_B]>250)     i = 0;
  879.                 XSetFillStyle(dpy, gcf, FillSolid);
  880.                 d_fillcolor = i;
  881.                 setfillcolor(i);
  882.            }
  883.         if (cc.b[B_F] == 2)     /* grids and shades */
  884.            {
  885.                 if (cc.b[B_R]==0x00 && cc.b[B_G]==0x00 && cc.b[B_B]==0x20) j=0; /* SHADE  */
  886.                 if (cc.b[B_R]==0x04 && cc.b[B_G]==0x00 && cc.b[B_B]==0x0C) j=1; /* SHADE1 */
  887.                 if (cc.b[B_R]==0x00 && cc.b[B_G]==0x00 && cc.b[B_B]==0x10) j=2; /* SHADE2 */
  888.                 if (cc.b[B_R]==0x05 && cc.b[B_G]==0x00 && cc.b[B_B]==0x20) j=3; /* SHADE3 */
  889.                 if (cc.b[B_R]==0x10 && cc.b[B_G]==0x00 && cc.b[B_B]==0x40) j=4; /* SHADE4 */
  890.                 if (cc.b[B_R]==0x20 && cc.b[B_G]==0x00 && cc.b[B_B]==0x60) j=5; /* SHADE5 */
  891.                 if (cc.b[B_R]==0x00 && cc.b[B_G]==0x20 && cc.b[B_B]==0x20) j=6; /* GRID   */
  892.                 if (cc.b[B_R]==0x04 && cc.b[B_G]==0x0f && cc.b[B_B]==0x0f) j=7; /* GRID1  */
  893.                 if (cc.b[B_R]==0x00 && cc.b[B_G]==0x10 && cc.b[B_B]==0x10) j=8; /* GRID2  */
  894.                 if (cc.b[B_R]==0x05 && cc.b[B_G]==0x20 && cc.b[B_B]==0x20) j=9; /* GRID3  */
  895.                 if (cc.b[B_R]==0x10 && cc.b[B_G]==0x40 && cc.b[B_B]==0x40) j=10;/* GRID4  */
  896.                 if (cc.b[B_R]==0x20 && cc.b[B_G]==0x60 && cc.b[B_B]==0x60) j=11;/* GRID5  */
  897.                 setfillcolor(1); /* black */
  898.             setfillstyle(j);
  899.            }
  900.         else
  901.            {
  902.                   XSetFillStyle(dpy, gcf, FillSolid);
  903.                 setfillcolor(d_fillcolor);
  904.            }
  905. }
  906. /*---------------------------------------------------------------------------*/
  907. d_beginclip()
  908. {
  909. }
  910. d_endclip()
  911. {
  912. }
  913. struct char_data {float wx,wy,x1,y1,x2,y2; };
  914. int font_get_chardata(struct char_data **cd, int ff, int cc);
  915. /*---------------------------------------------------------------------------*/
  916. int safnt;
  917. int simple_char(int cc);
  918. d_char(int font, int cc)
  919. {
  920.     static struct char_data cd;
  921.     static int ix1,ix2,iy1,iy2;
  922.     static int ux,uy;
  923.     char ss[2];
  924.  
  925.     in_font = true;
  926.     ss[0] = cc;
  927.     ss[1] = 0;
  928.     if (safnt==0) safnt = pass_font("PLSR");
  929.     if (font_get_encoding(font)>2) {
  930.         my_char(font,cc);
  931.         in_font = false;
  932.         return;
  933.     }
  934.     my_char(safnt,cc);
  935.     in_font = false;
  936. }
  937.  
  938.  
  939. int wait_expose()
  940. {
  941.     /* Wait till he presses key or clicks mouse */
  942.     XEvent ereturn;
  943.     int32 emask;
  944.  
  945. /*    for (;;) {
  946.         XNextEvent(dpy,&ereturn);
  947.         wprintf("Event %d  %d %d %d\n",ereturn.type,Expose,ButtonPress,
  948.             VisibilityNotify);
  949.         scr_refresh();
  950.     }
  951. */
  952. /*     XSync(dpy,True);    */
  953.         emask = ExposureMask | ButtonPressMask | KeyPressMask;
  954.     XWindowEvent(dpy, window1, emask, &ereturn);
  955. }
  956.  
  957. int set_expose()
  958. {
  959.    XEvent ereturn;
  960.    int32 emask;
  961.  
  962.    emask = ExposureMask | ButtonPressMask | KeyPressMask
  963.             | VisibilityChangeMask;
  964.  
  965. /* #define X11R5 true for X11R5 and R6 */
  966. #define X11R5 true
  967. #ifdef X11R5
  968.     XSelectInput(dpy, window1, emask);
  969. #else
  970.     XSelectInput(dpy, window1, emask, &ereturn);
  971. #endif
  972. }
  973.  
  974.  
  975. /*-------------------------------------------------------------------------*/
  976. /*            path routines for x windows */
  977. #define PATH_LENGTH 500
  978. struct Pnt {int type,x,y;} ;
  979. static struct Pnt pnts[PATH_LENGTH];
  980. static int npnts;
  981. enum {P_MOVE,P_LINE,P_BEZIER};
  982. static int startx,starty;
  983. void path_move(int x, int y)
  984. {
  985.     startx = x; starty = y;
  986.     pnts[npnts].type = P_MOVE;
  987.     pnts[npnts].x = x;
  988.     pnts[npnts++].y = y;
  989. }
  990. void path_line(int x, int y)
  991. {
  992.     pnts[npnts].type = P_LINE;
  993.     pnts[npnts].x = x;
  994.     pnts[npnts++].y = y;
  995. }
  996. void path_close(void)
  997. {
  998.     pnts[npnts].type = P_LINE;
  999.     pnts[npnts].x = startx;
  1000.     pnts[npnts++].y = starty;
  1001. }
  1002. void path_fill(void)
  1003. {
  1004.     XPoint pts[PATH_LENGTH];
  1005.     int i,npts;
  1006.  
  1007.     for (i=0; i<npnts; i++) {
  1008.         if (pnts[i].type==P_LINE) {
  1009.             npts = 0;
  1010.             pts[npts].x = pnts[i].x;
  1011.             pts[npts++].y = pnts[i++].y;
  1012.             for (;pnts[i].type==P_LINE && i<npnts;i++) {
  1013.                 pts[npts].x = pnts[i].x;
  1014.                 pts[npts++].y = pnts[i].y;
  1015.             }
  1016.                XFillPolygon(dpy,window1,gcf,pts,npts,Complex, CoordModeOrigin);
  1017.         }
  1018.     }
  1019. }
  1020. void path_newpath(void)
  1021. {
  1022.     npnts = 0;
  1023. }
  1024. void path_stroke(void)
  1025. {
  1026.     for (i=1; i<npnts; i++) {
  1027.         if (pnts[i].type==P_LINE) {
  1028.                 XDrawLine(dpy,window1,gc,
  1029.                 pnts[i-1].x,pnts[i-1].y,
  1030.                 pnts[i].x,pnts[i].y);
  1031.         }
  1032.     }
  1033. }
  1034.  
  1035.